package com.wuming.house.controller;

import com.wuming.house.core.CommonResult;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.Banner;
import com.wuming.house.model.GoodImage;
import com.wuming.house.service.BannerService;
import com.wuming.house.service.GoodImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/web/banner")
public class BannerController extends BaseController {

    @Autowired
    private BannerService bannerService;

    @Autowired
    private GoodImageService goodImageService;

    @RequestMapping("/toManageBanner")
    public String toManageBanner() {
        return "/banner/manageBanner";
    }

    @RequestMapping("/toAddBanner")
    public String toAddBanner() {
        return "/banner/editBanner";
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam(defaultValue = "1") int pageIndex, @RequestParam(defaultValue = "10") int pageSize) {
        try {
            List<Banner> banners = bannerService.getBannerByPage(pageIndex, pageSize);
            int total = bannerService.getTotal();
            return CommonResult.createSuccessResult(banners, total, "查询所有广告成功");
        } catch (Exception e) {
            logger.error("发生业务异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("查询所有广告失败");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public JsonResult delete(@PathVariable Long id) {
        logger.info("准备删除id:{}的广告", id);
        try {
            bannerService.removeBannerById(id);
            return new JsonResult(null, "删除成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("发生业务异常,异常信息:{}", e.getMessage());
            return new JsonResult(null, "删除失败", false);
        }
    }


    @ResponseBody
    @RequestMapping("/uploadImg")
    public JsonResult uploadImg(MultipartFile file) {
        try {
            String path = this.uploadImage(file, "banner", 1);
            GoodImage goodImage = new GoodImage();
            goodImage.setImagePath(path);
            goodImage.setCreateDatetime(new Date());
            goodImageService.saveImage(goodImage);
            return new JsonResult(goodImage.getId(), "上传成功", true);
        } catch (IOException ie) {
            logger.error("上传广告图片失败,异常:{}", ie.getMessage());
            return new JsonResult("", "上传失败", false);
        } catch (Exception e) {
            logger.error("上传广告图片失败,异常:{}", e.getMessage());
            return new JsonResult("", "上传失败", false);
        }
    }

    @ResponseBody
    @RequestMapping("/save")
    public JsonResult save(Banner banner) {
        try {
            if (null == banner.getId()) {
                banner.setCreateDatetime(new Date());
                GoodImage gi = goodImageService.getImageById(banner.getPicId());
                banner.setPicUrl(gi.getImagePath());
                bannerService.saveBanner(banner);
            } else {
                bannerService.modifyBanner(banner);
            }
            return new JsonResult(banner, "保存成功", true);
        } catch (Exception e) {
            logger.error("保存失败,异常:{}", e.getMessage());
            return new JsonResult(null, "保存失败", false);
        }
    }

}
